package com.joyplus.report.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by  2018/4/23.
 */
public class DateUitls {


  public static Date strToDate(String dateStr, String pattern) {
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    try {
      return format.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static String formatToStr(Date date, String pattern) {
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    return format.format(date);
  }

 /* public static void main(String[] args) {
    System.out.println(strToDate("2018/04/28","yyyy/MM/dd"));
  }*/


  public static List<String> findDates(String start, String end) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date dBegin = null;
    Date dEnd = null;
    try {
      dBegin = sdf.parse(start);
      dEnd = sdf.parse(end);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    List lDate = new ArrayList();
    lDate.add(start);
    Calendar calBegin = Calendar.getInstance();
    // 使用给定的 Date 设置此 Calendar 的时间
    calBegin.setTime(dBegin);
    Calendar calEnd = Calendar.getInstance();
    // 使用给定的 Date 设置此 Calendar 的时间
    calEnd.setTime(dEnd);
    // 测试此日期是否在指定日期之后
    while (dEnd.after(calBegin.getTime())) {
      // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
      calBegin.add(Calendar.DAY_OF_MONTH, 1);
      lDate.add(sdf.format(calBegin.getTime()));
    }
    return lDate;
  }


  public static String firstDay() {
    SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
//获取前月的第一天
    //获取当前日期
    Calendar cal1 = Calendar.getInstance();
    cal1.add(Calendar.MONTH, -0);
    //设置为1号,当前日期既为本月第一天
    cal1.set(Calendar.DAY_OF_MONTH, 1);
    String firstDay = format.format(cal1.getTime());
    return firstDay;
  }

  public static String beforeOneHourToNowDate() {
    Calendar calendar = Calendar.getInstance();
    /* HOUR_OF_DAY 指示一天中的小时 */
    calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH");
    System.out.println("一个小时前的时间：" + df.format(calendar.getTime()));
    System.out.println("当前的时间：" + df.format(new Date()));
    return df.format(calendar.getTime());
  }

  /**
   * Java将Unix时间戳转换成指定格式日期字符串
   * @param timestampString 时间戳 如："1473048265";
   * @param formats 要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
   *
   * @return 返回结果 如："2016-09-05 16:06:42";
   */
  public static String TimeStamp2Date(String timestampString, String formats) {
    Long timestamp = Long.parseLong(timestampString) * 1000;
    String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
    return date;
  }
  /**
   * 获取精确到秒的时间戳
   * @return
   */
  public static int getSecondTimestamp(Date date){
    if (null == date) {
      return 0;
    }
    String timestamp = String.valueOf(date.getTime());
    int length = timestamp.length();
    if (length > 3) {
      return Integer.valueOf(timestamp.substring(0,length-3));
    } else {
      return 0;
    }
  }
  public static void main(String[] args) throws Exception {

    System.out.println(TimeStamp2Date("1527651917","yyyy-MM-dd HH:mm:ss"));


    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2018-06-12 00:00:00"));
    /* HOUR_OF_DAY 指示一天中的小时 */
    calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH");
    System.out.println("一个小时前的时间：" + df.format(calendar.getTime()));
    System.out.println("当前的时间：" + df.format(new Date()));

   /* Calendar cal = Calendar.getInstance();
    String start = "2012-04-01";
    String end = "2012-04-10";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date dBegin = sdf.parse(start);
    Date dEnd = sdf.parse(end);
    List<Date> lDate = findDates(dBegin, dEnd);
    for (Date date : lDate) {
      System.out.println(sdf.format(date));
    }*/
  }
}
